package com.example.dict.GUI;

import com.example.dict.data.DictionaryStorage;
import com.example.dict.utils.Dictionary.Operations.Hash;
import com.example.dict.utils.Dictionary.dictionaryOperations;
import com.example.dict.utils.Dictionary.Operations.AvlTree;
import com.example.dict.utils.util.Record;

import java.io.*;
import java.util.StringTokenizer;

import static com.example.dict.data.staticFilterData.MENU_OPTIONS;
import static com.example.dict.data.staticFilterData.WORD_CHECKER;

public class commands {

    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer st = new StringTokenizer("");
    static dictionaryOperations dictionary = Hash.acquire();



    /**
     * 命令行窗口
     */
    public static void main(String[] args) throws IOException {
        while (true) {
            switch (getCommand()) {
                case 1:
                    translateService();
                    break;
                case 2:
                    recordService();
                    break;
                case 3:
                    removeService();
                    break;
                default:
                    System.out.println("已退出");
                    System.exit(0);
                    break;
            }
        }
    }


    /**
     * 获取指令
     *
     * @return 指令
     */
    public static int getCommand() throws IOException {

        /* 命令行输出 */
        System.out.println("1.翻译单词");
        System.out.println("2.补充词库");
        System.out.println("3.删除单词");
        System.out.println("按其余键退出");
        System.out.print("请输入（1 - 3）：");
        String command = next();

        if (!command.matches(MENU_OPTIONS)) {
            return 0;
        }

        return Integer.parseInt(command);
    }

    /**
     * 翻译业务方法
     */
    public static void translateService() throws IOException {
        System.out.println("==========要翻译的单词==========");
        String word = next();
        if (!word.matches(WORD_CHECKER)) {
            System.out.println("ERROR: 无效输入!");
            return;
        }
        String translation = dictionary.translate(word.toLowerCase());
        System.out.println("============翻译结果============");
        System.out.println((null == translation) ? "单词还未收录，您可以添加" : translation);
        System.out.println("===============================");
    }

    /**
     * 新增业务方法
     */
    public static void recordService() throws IOException {

        System.out.println("==========要添加的单词==========");
        String word = next();

        if (!word.matches(WORD_CHECKER)) {
            System.out.println("ERROR: 无效输入!");
            return;
        }
        word = word.toLowerCase();
        String translation = dictionary.translate(word);

        if (null != translation) {
            System.out.println("===============================");
            System.out.println("单词已收录：" + translation);
            System.out.println("===============================");
            return;
        }

        System.out.println("==============翻译=============");
        dictionary.insert(word, translation = next());
        System.out.println("==============成功=============");
        DictionaryStorage ds = new DictionaryStorage();
        ds.storage(new Record(word, translation));
    }

    /**
     * 删除业务方法
     */
    public static void removeService() throws IOException {

        System.out.println("==========要删除的单词==========");
        String word = next();

        if (!word.matches(WORD_CHECKER)) {
            System.out.println("ERROR: 无效输入!");
            return;
        }
        word = word.toLowerCase();

        String translation = dictionary.translate(word);

        if (null == translation) {
            System.out.println("===============================");
            System.out.println("单词未收录：");
            System.out.println("===============================");
            return;
        }

        dictionary.delete(word);
        System.out.println("==============成功=============");
        DictionaryStorage ds = new DictionaryStorage();
        ds.deleteRecord(word);
    }


    /**
     * @return 用户输入的字符串
     * */
    static String next() throws IOException {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
